package cn.lls.service.impl;

import cn.lls.base.exception.LlsException;
import cn.lls.base.result.ResultCode;
import cn.lls.dao.WageDao;
import cn.lls.entity.company.WageEntity;
import cn.lls.entity.sys.EmployeeBaseEntity;
import cn.lls.excel.WageExcel;
import cn.lls.req.emp.ConditionSearchWageExcelReq;
import cn.lls.req.emp.ConditionSearchWageReq;
import cn.lls.req.emp.PersonWageReq;
import cn.lls.req.emp.WageReq;
import cn.lls.service.IEmployeeBaseService;
import cn.lls.service.IWageService;
import cn.lls.vo.company.CompanyAllInfo;
import cn.lls.vo.emp.WageInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 划水艺术家
 * @since 2022-11-15
 */
@Service
public class WageServiceImpl extends ServiceImpl<WageDao, WageEntity> implements IWageService {

    @Autowired
    private IEmployeeBaseService employeeBaseService;

    @Override
    public IPage<WageInfo> getWageInfoByPageAndCondition(ConditionSearchWageReq conditionSearchWageReq) {
        IPage<CompanyAllInfo> page = new Page<>(conditionSearchWageReq.getCurrentPage(), conditionSearchWageReq.getPageSize());
        return this.baseMapper.getWageInfoByPageAndCondition(page, conditionSearchWageReq);
    }

    @Override
    public List<WageExcel> getWageExcel(ConditionSearchWageExcelReq conditionSearchWageReq) {
        return this.baseMapper.getWageInfoExcel(conditionSearchWageReq);
    }

    @Override
    public WageInfo getWageById(Long wageId) {
        return this.baseMapper.getWageById(wageId);
    }

    @Override
    public void saveWage(WageReq wageReq) {
        Long userId = getUserIdByNameAndPhoneAndDepartment(wageReq.getEmployeeName(), wageReq.getEmployeeTel(), wageReq.getDepartmentId());
        WageEntity wageEntity = new WageEntity();
        BeanUtils.copyProperties(wageReq, wageEntity);
        wageEntity.setUserId(userId);
        this.baseMapper.insert(wageEntity);
    }

    @Override
    public void updateWage(WageReq wageReq) {
        Long userId = getUserIdByNameAndPhoneAndDepartment(wageReq.getEmployeeName(), wageReq.getEmployeeTel(), wageReq.getDepartmentId());
        WageEntity wageEntity = new WageEntity();
        BeanUtils.copyProperties(wageReq, wageEntity);
        wageEntity.setUserId(userId);
        this.baseMapper.updateById(wageEntity);
    }

    @Override
    public IPage<WageInfo> getWageByEmpId(PersonWageReq personWageReq) {
        IPage<CompanyAllInfo> page = new Page<>(personWageReq.getCurrentPage(), personWageReq.getPageSize());
        return this.baseMapper.getWageByEmpId(page, personWageReq.getUserId());
    }


    /*
    根据 部门、姓名、手机号获取员工 id
     */
    public Long getUserIdByNameAndPhoneAndDepartment(String name, String phone, Long departmentId){
        LambdaQueryWrapper<EmployeeBaseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeBaseEntity::getEmployeeName, name);
        queryWrapper.eq(EmployeeBaseEntity::getEmployeeTel, phone);
        queryWrapper.eq(EmployeeBaseEntity::getDepartmentId, departmentId);
        EmployeeBaseEntity one = employeeBaseService.getOne(queryWrapper);
        if (one == null)
            throw new LlsException(ResultCode.DATA_ERROR, "当前部门不存在所填员工，请检查姓名与手机号");
        return one.getUserId();
    }
}
